<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>Dubbo 服务暴露 | flyingzc's blog</title><meta name="author" content="flyingzc"><meta name="copyright" content="flyingzc"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Dubbo 与 Spring 的结合代码在 dubbo-config 下的 dubbo-config-spring 模块中  META-INF&#x2F;dubbo.xsddubbo.xsd 定义了 dubbo 自定义标签 META-INF&#x2F;spring.schemasspring.schemas 指定了 dubbo.xsd 文件的位置. 1http\:&#x2F;&#x2F;code.alibabatec">
<meta property="og:type" content="article">
<meta property="og:title" content="Dubbo 服务暴露">
<meta property="og:url" content="https://flyingzc.github.io/articles/dubbo/dubbo03-export.html">
<meta property="og:site_name" content="flyingzc&#39;s blog">
<meta property="og:description" content="Dubbo 与 Spring 的结合代码在 dubbo-config 下的 dubbo-config-spring 模块中  META-INF&#x2F;dubbo.xsddubbo.xsd 定义了 dubbo 自定义标签 META-INF&#x2F;spring.schemasspring.schemas 指定了 dubbo.xsd 文件的位置. 1http\:&#x2F;&#x2F;code.alibabatec">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://flyingzc.github.io/images/zc-icon.jpeg">
<meta property="article:published_time" content="2019-11-28T16:00:00.000Z">
<meta property="article:modified_time" content="2025-01-21T01:51:41.210Z">
<meta property="article:author" content="flyingzc">
<meta property="article:tag" content="Dubbo">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://flyingzc.github.io/images/zc-icon.jpeg"><script type="application/ld+json">{
  "@context": "https://schema.org",
  "@type": "BlogPosting",
  "headline": "Dubbo 服务暴露",
  "url": "https://flyingzc.github.io/articles/dubbo/dubbo03-export.html",
  "image": "https://flyingzc.github.io/images/zc-icon.jpeg",
  "datePublished": "2019-11-28T16:00:00.000Z",
  "dateModified": "2025-01-21T01:51:41.210Z",
  "author": [
    {
      "@type": "Person",
      "name": "flyingzc",
      "url": "https://flyingzc.github.io/"
    }
  ]
}</script><link rel="shortcut icon" href="/images/zc-icon.jpeg"><link rel="canonical" href="https://flyingzc.github.io/articles/dubbo/dubbo03-export.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta name="google-site-verification" content="DentpX3LnP_WNgEeFnNIcTZZky-Bby-zL-uI_9GqTAw"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css"><script>
    (() => {
      
    const saveToLocal = {
      set: (key, value, ttl) => {
        if (!ttl) return
        const expiry = Date.now() + ttl * 86400000
        localStorage.setItem(key, JSON.stringify({ value, expiry }))
      },
      get: key => {
        const itemStr = localStorage.getItem(key)
        if (!itemStr) return undefined
        const { value, expiry } = JSON.parse(itemStr)
        if (Date.now() > expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return value
      }
    }

    window.btf = {
      saveToLocal,
      getScript: (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        Object.entries(attr).forEach(([key, val]) => script.setAttribute(key, val))
        script.onload = script.onreadystatechange = () => {
          if (!script.readyState || /loaded|complete/.test(script.readyState)) resolve()
        }
        script.onerror = reject
        document.head.appendChild(script)
      }),
      getCSS: (url, id) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onload = link.onreadystatechange = () => {
          if (!link.readyState || /loaded|complete/.test(link.readyState)) resolve()
        }
        link.onerror = reject
        document.head.appendChild(link)
      }),
      addGlobalFn: (key, fn, name = false, parent = window) => {
        if (!false && key.startsWith('pjax')) return
        const globalFn = parent.globalFn || {}
        globalFn[key] = globalFn[key] || {}
        globalFn[key][name || Object.keys(globalFn[key]).length] = fn
        parent.globalFn = globalFn
      }
    }
  
      
      const activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      const activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }

      btf.activateDarkMode = activateDarkMode
      btf.activateLightMode = activateLightMode

      const theme = saveToLocal.get('theme')
    
          theme === 'dark' ? activateDarkMode() : theme === 'light' ? activateLightMode() : null
        
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        document.documentElement.classList.toggle('hide-aside', asideStatus === 'hide')
      }
    
      
    const detectApple = () => {
      if (/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)) {
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
  
    })()
  </script><script async="async" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><script>(adsbygoogle = window.adsbygoogle || []).push({
  google_ad_client: '',
  enable_page_level_ads: 'true'
});</script><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false,"highlightFullpage":false,"highlightMacStyle":false},
  copy: {
    success: '复制成功',
    error: '复制失败',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'null',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyloadPlugin: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Dubbo 服务暴露',
  isHighlightShrink: false,
  isToc: true,
  pageType: 'post'
}</script><link rel="stylesheet" href="/self/github-dark.css"><meta name="generator" content="Hexo 7.0.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img text-center"><img src="/images/zc-icon.jpeg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data text-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">84</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">36</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">29</div></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header"><nav id="nav"><span id="blog-info"><a class="nav-site-title" href="/"><span class="site-name">flyingzc's blog</span></a><a class="nav-page-title" href="/"><span class="site-name">Dubbo 服务暴露</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><span class="site-page"><i class="fas fa-bars fa-fw"></i></span></div></div></nav><div id="post-info"><h1 class="post-title">Dubbo 服务暴露</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2019-11-28T16:00:00.000Z" title="发表于 2019-11-29 00:00:00">2019-11-29</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2025-01-21T01:51:41.210Z" title="更新于 2025-01-21 09:51:41">2025-01-21</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Dubbo/">Dubbo</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title=""><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">浏览量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="container post-content" id="article-container"><h1 id="Dubbo-与-Spring-的结合"><a href="#Dubbo-与-Spring-的结合" class="headerlink" title="Dubbo 与 Spring 的结合"></a>Dubbo 与 Spring 的结合</h1><p>代码在 dubbo-config 下的 dubbo-config-spring 模块中</p>
<p><img src="https://gitee.com/flyingzc/MyImg/raw/master/img/dubbo/Dubbo-Config%E7%B1%BB%E5%9B%BE.png" alt="Dubbo Config整体类图"></p>
<h2 id="META-INF-dubbo-xsd"><a href="#META-INF-dubbo-xsd" class="headerlink" title="META-INF&#x2F;dubbo.xsd"></a>META-INF&#x2F;dubbo.xsd</h2><p>dubbo.xsd 定义了 dubbo 自定义标签</p>
<h2 id="META-INF-spring-schemas"><a href="#META-INF-spring-schemas" class="headerlink" title="META-INF&#x2F;spring.schemas"></a>META-INF&#x2F;spring.schemas</h2><p>spring.schemas 指定了 dubbo.xsd 文件的位置.</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs plaintext">http\://code.alibabatech.com/schema/dubbo/dubbo.xsd=META-INF/dubbo.xsd<br></code></pre></td></tr></table></figure>

<h2 id="META-INF-spring-handlers"><a href="#META-INF-spring-handlers" class="headerlink" title="META-INF&#x2F;spring.handlers"></a>META-INF&#x2F;spring.handlers</h2><p>关联 DubboNamespaceHandler 命名空间处理器和 dubbo.xsd 中的 targetNamespace,即 <code>http://code.alibabatech.com/schema/dubbo</code></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs plaintext">http\://code.alibabatech.com/schema/dubbo=com.alibaba.dubbo.config.spring.schema.DubboNamespaceHand<br></code></pre></td></tr></table></figure>

<h2 id="DubboNamespaceHandler-init"><a href="#DubboNamespaceHandler-init" class="headerlink" title="DubboNamespaceHandler.init()"></a>DubboNamespaceHandler.init()</h2><p>DubboNamespaceHandler.init(): 注册各个自定义元素的解析器,所有元素都是交给 DubboBeanDefinitionParser 处理的,传入不同的 Class 对象</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">init</span><span class="hljs-params">()</span> &#123; <span class="hljs-comment">// 注册BeanDefinitionParser用来解析dubbo定义的那些xml元素节点</span><br>    registerBeanDefinitionParser(<span class="hljs-string">&quot;application&quot;</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">DubboBeanDefinitionParser</span>(ApplicationConfig.class, <span class="hljs-literal">true</span>));<span class="hljs-comment">// &lt;dubbo:application name=&quot;dubbo-admin&quot; /&gt;</span><br>    registerBeanDefinitionParser(<span class="hljs-string">&quot;module&quot;</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">DubboBeanDefinitionParser</span>(ModuleConfig.class, <span class="hljs-literal">true</span>));<span class="hljs-comment">// &lt;dubbo:registry address=&quot;$&#123;dubbo.registry.address&#125;&quot; check=&quot;false&quot; file=&quot;false&quot; /&gt;</span><br>    registerBeanDefinitionParser(<span class="hljs-string">&quot;registry&quot;</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">DubboBeanDefinitionParser</span>(RegistryConfig.class, <span class="hljs-literal">true</span>));<span class="hljs-comment">// &lt;dubbo:reference id=&quot;registryService&quot; interface=&quot;com.alibaba.dubbo.registry.RegistryService&quot; check=&quot;false&quot; /&gt;</span><br>    registerBeanDefinitionParser(<span class="hljs-string">&quot;monitor&quot;</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">DubboBeanDefinitionParser</span>(MonitorConfig.class, <span class="hljs-literal">true</span>));<br>    registerBeanDefinitionParser(<span class="hljs-string">&quot;provider&quot;</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">DubboBeanDefinitionParser</span>(ProviderConfig.class, <span class="hljs-literal">true</span>));<br>    registerBeanDefinitionParser(<span class="hljs-string">&quot;consumer&quot;</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">DubboBeanDefinitionParser</span>(ConsumerConfig.class, <span class="hljs-literal">true</span>));<br>    registerBeanDefinitionParser(<span class="hljs-string">&quot;protocol&quot;</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">DubboBeanDefinitionParser</span>(ProtocolConfig.class, <span class="hljs-literal">true</span>));<br>    <span class="hljs-comment">//  &lt;dubbo:service  interface=&quot;com.alibaba.dubbo.demo.DemoService&quot; group=&quot;g1&quot; ref=&quot;demoService&quot; filter=&quot;demo&quot; deprecated=&quot;false&quot; callbacks=&quot;1000&quot; timeout=&quot;200000&quot; accesslog=&quot;true&quot;&gt; 解析成 ServiceBean</span><br>    registerBeanDefinitionParser(<span class="hljs-string">&quot;service&quot;</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">DubboBeanDefinitionParser</span>(ServiceBean.class, <span class="hljs-literal">true</span>));<br>    registerBeanDefinitionParser(<span class="hljs-string">&quot;reference&quot;</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">DubboBeanDefinitionParser</span>(ReferenceBean.class, <span class="hljs-literal">false</span>));<br><br>    registerBeanDefinitionParser(<span class="hljs-string">&quot;annotation&quot;</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">AnnotationBeanDefinitionParser</span>()); <span class="hljs-comment">// 废弃</span><br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="DubboBeanDefinitionParser"><a href="#DubboBeanDefinitionParser" class="headerlink" title="DubboBeanDefinitionParser"></a>DubboBeanDefinitionParser</h2><p>DubboBeanDefinitionParser.parse()中<br>根据 beanClass 创建 RootBeanDefinition,设置为非懒加载.<br>若未配置 id 属性,则会根据 name 或 interface 属性生成,并通过判断 BeanRegistry 中是否存在该 id 的 bean,用 则增序列解决重复问题.<br>通过id注册到BeanRegistry中,并设置 beanDefinition 的 id 属性.<br>下面就是对 ServiceBean 等元素的各自的属性配置的处理.</p>
<p>在Spring应用上下文启动过程中,加载XML配置文件(比如提供者配置和消费者配置文件)为 Resource 对象,并通过 XmlBeanDefinitionReader 调用BeanDefinitionParser 解析 XML 标签为 BeanDefinition,并注册到 BeanDefinitionRegistry 中.</p>
<h1 id="ServiceBean"><a href="#ServiceBean" class="headerlink" title="ServiceBean"></a>ServiceBean</h1><p>ServiceBean 实现了 InitializingBean, DisposableBean, ApplicationContextAware, <code>ApplicationListener&lt;ContextRefreshedEvent&gt;</code>, BeanNameAware 这几个接口.</p>
<ol>
<li><p>实现的 ApplicationContextAware 接口的 setApplicationContext() 方法中,会:<br>保存 applicationContext 引用.<br>applicationContext 引用 保存到 SpringExtensionFactory 中.</p>
</li>
<li><p>实现的 InitializingBean 接口的 afterPropertiesSet() 方法中,会: 设置相关属性.</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs java">- ServiceBean.afterPropertiesSet()<br>    - 从BeanFactory中获取ProviderConfig,ProtocolConfig等,这些bean是之前在Spring bean解析时加载进来的.将这些配置对象赋值到ServiceBean中,当然也有可能在其父类中.<br></code></pre></td></tr></table></figure>
</li>
<li><p>实现的 <code>ApplicationListener&lt;ContextRefreshedEvent&gt;</code> 接口的 onApplicationEvent(ContextRefreshedEvent event) 方法中,有服务暴露的相关逻辑.</p>
</li>
</ol>
<h2 id="服务本地暴露"><a href="#服务本地暴露" class="headerlink" title="服务本地暴露"></a>服务本地暴露</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs plaintext">- ServiceBean.onApplicationEvent()<br>    监听spring容器初始化完成时触发<br>    - ServiceConfig.doExport()根据配置选择立即暴露或延迟暴露<br>        - 暴露服务doExportUrls()<br>            - loadRegistries(true)加载注册中心 URL 数组<br>                - 将xml中配置的&lt;dubbo:registry&gt;解析成url对象(如registry://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&amp;dubbo=2.0.0&amp;logger=jcl&amp;pid=52008&amp;qos.port=22222&amp;registry=zookeeper&amp;timestamp=1574666370052)<br>            - doExportUrlsFor1Protocol(protocolConfig, registryURLs)根据不同的协议把服务export到不同的注册中心(如zk)上去<br>                - ServiceConfig.exportLocal()中:服务本地暴露<br>                    - 1.通过传入的协议url构造本地injvm协议的url<br>                    - 2.通过proxyFactory.getInvoker()获取Invoker<br>                        - ProxyFactory$Adaptive.getInvoker()方法<br>                        - 1.ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.rpc.ProxyFactory.class).getExtension(&quot;javassist&quot;)获取StubProxyFactoryWrapper 对象<br>                        - 2.extension.getInvoker(arg0, arg1, arg2)通过ProxyFactory获取Invoker对象<br>                            - StubProxyFactoryWrapper.getInvoker()直接调用内部的JavassistProxyFactory.getInvoker()<br>                                - JavassistProxyFactory.getInvoker()方法中<br>                                - 1.根据proxy对象创建Wrapper<br>                                - 2.根据proxy, type, url创建AbstractProxyInvoker对象,它的doInvoke()方法会调用wrapper类的invokeMethod()方法<br>                    - 3.protocol.export()将Invoker转为Exporter<br>                        - Protocol$Adaptive.export()生成的代理类<br>                            - 1.通过ExtensionLoader获取name为injvm的Protocol拓展对象.吐出的是ProtocolListenerWrapper的实例,包裹了InjvmProtocol<br>                            - 2.extension.export(invoker)<br>                                - ProtocolFilterWrapper.export()中<br>                                    - 1.buildInvokerChain()建立带有 Filter 过滤链的 Invoker,再暴露服务<br>                                    - 2.protocol.export()<br>                                        - ProtocolListenerWrapper.export()中<br>                                        - 1.通过InjvmProtocol.export()把invoker暴露为exporter<br>                                            - InjvmProtocol.export()中<br>                                            - 1.创建InjvmExporter对象<br>                                        - 2.获取ExporterListener拓展对象列表<br>                                        - 3.创建ListenerExporterWrapper对象作为exporter返回<br>                <br></code></pre></td></tr></table></figure>

<p>ServiceConfig.exportLocal()中:服务本地暴露</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">exportLocal</span><span class="hljs-params">(URL url)</span> &#123;<br>    <span class="hljs-keyword">if</span> (!Constants.LOCAL_PROTOCOL.equalsIgnoreCase(url.getProtocol())) &#123; <span class="hljs-comment">// 协议不以injvm开头</span><br>        <span class="hljs-comment">// 1.创建本地 injvm URL</span><br>        <span class="hljs-type">URL</span> <span class="hljs-variable">local</span> <span class="hljs-operator">=</span> URL.valueOf(url.toFullString())<br>                .setProtocol(Constants.LOCAL_PROTOCOL) <span class="hljs-comment">// injvm</span><br>                .setHost(LOCALHOST) <span class="hljs-comment">// 本地</span><br>                .setPort(<span class="hljs-number">0</span>); <span class="hljs-comment">// 端口=0</span><br>        ServiceClassHolder.getInstance().pushServiceClass(getServiceClass(ref));<br>        <span class="hljs-comment">// 2.使用 ProxyFactory 创建 Invoker 对象</span><br>        <span class="hljs-comment">// 3.使用 Protocol 暴露 Invoker 对象,将 Invoker 转成 exporter</span><br>        Exporter&lt;?&gt; exporter = protocol.export(proxyFactory.getInvoker(ref, (Class) interfaceClass, local));<br>        <span class="hljs-comment">// 添加到 `exporters`</span><br>        exporters.add(exporter);<br>        logger.info(<span class="hljs-string">&quot;Export dubbo service &quot;</span> + interfaceClass.getName() + <span class="hljs-string">&quot; to local registry&quot;</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>这个 proxyFactory 是 ProxyFactory$Adaptive 对象,会根据传入的参数创建具体的 proxyFactory 对象.<br>proxyFactory.getInvoker(ref, (Class) interfaceClass, local)传入的参数分别是 服务具体实现类对象ref,服务接口DemoService,本地暴露的URL对象</p>
<p>ProxyFactory$Adaptive.getInvoker()方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> com.alibaba.dubbo.rpc.Invoker <span class="hljs-title function_">getInvoker</span><span class="hljs-params">(java.lang.Object arg0, java.lang.Class arg1, com.alibaba.dubbo.common.URL arg2)</span> <span class="hljs-keyword">throws</span> com.alibaba.dubbo.rpc.RpcException &#123;<span class="hljs-comment">// 获取Invoker.参数 arg0 为 ref 对象, arg1 为 服务接口(如 DemoService), args 为 服务暴露的 URL</span><br>    <span class="hljs-keyword">if</span> (arg2 == <span class="hljs-literal">null</span>) <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">IllegalArgumentException</span>(<span class="hljs-string">&quot;url == null&quot;</span>);<br>    com.alibaba.dubbo.common.<span class="hljs-type">URL</span> <span class="hljs-variable">url</span> <span class="hljs-operator">=</span> arg2;<br>    <span class="hljs-type">String</span> <span class="hljs-variable">extName</span> <span class="hljs-operator">=</span> url.getParameter(<span class="hljs-string">&quot;proxy&quot;</span>, <span class="hljs-string">&quot;javassist&quot;</span>);<span class="hljs-comment">// 从url参数中获取 ProxyFactory 拓展使用的方式,默认是javassist</span><br>    <span class="hljs-keyword">if</span>(extName == <span class="hljs-literal">null</span>) <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">IllegalStateException</span>(<span class="hljs-string">&quot;Fail to get extension(com.alibaba.dubbo.rpc.ProxyFactory) name from url(&quot;</span> + url.toString() + <span class="hljs-string">&quot;) use keys([proxy])&quot;</span>);<br>    com.alibaba.dubbo.rpc.<span class="hljs-type">ProxyFactory</span> <span class="hljs-variable">extension</span> <span class="hljs-operator">=</span> (com.alibaba.dubbo.rpc.ProxyFactory)ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.rpc.ProxyFactory.class).getExtension(extName); <span class="hljs-comment">// 获取 ProxyFactory 接口 的拓展实现(默认使用的实现类为 JavassistProxyFactory)</span><br>    <span class="hljs-keyword">return</span> extension.getInvoker(arg0, arg1, arg2);<span class="hljs-comment">// extension为StubProxyFactoryWrapper实例,包裹了JavassistProxyFactory对象</span><br>&#125;<br></code></pre></td></tr></table></figure>
<p>通过 <code>ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.rpc.ProxyFactory.class).getExtension(&quot;javassist&quot;)</code> 获取的是 JavassistProxyFactory 对象,同时 ProxyFactory 有个 Wrapper 实现类 StubProxyFactoryWrapper,会被 SPI AOP 包装到 JavassistProxyFactory 对象 外层,所以最后吐出的是 StubProxyFactoryWrapper 对象.</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> &lt;T&gt; Invoker&lt;T&gt; <span class="hljs-title function_">getInvoker</span><span class="hljs-params">(T proxy, Class&lt;T&gt; type, URL url)</span> &#123;<br>    <span class="hljs-comment">// 1.根据传入的 proxy 对象的类信息创建对它的包装对象 Wrapper</span><br>    <span class="hljs-keyword">final</span> <span class="hljs-type">Wrapper</span> <span class="hljs-variable">wrapper</span> <span class="hljs-operator">=</span> Wrapper.getWrapper(proxy.getClass().getName().indexOf(<span class="hljs-string">&#x27;$&#x27;</span>) &lt; <span class="hljs-number">0</span> ? proxy.getClass() : type);<br>    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbstractProxyInvoker</span>&lt;T&gt;(proxy, type, url) &#123;<span class="hljs-comment">// 2.返回Invoker对象实例,这个invoker对象invoke()方法可以根据传入的invocation对象中包含的方法名,方法参数来调用proxy对象 返回调用结果</span><br>        <span class="hljs-meta">@Override</span><br>        <span class="hljs-keyword">protected</span> Object <span class="hljs-title function_">doInvoke</span><span class="hljs-params">(T proxy, String methodName,</span><br><span class="hljs-params">                                    Class&lt;?&gt;[] parameterTypes,</span><br><span class="hljs-params">                                    Object[] arguments)</span> <span class="hljs-keyword">throws</span> Throwable &#123;<br>            <span class="hljs-keyword">return</span> wrapper.invokeMethod(proxy, methodName, parameterTypes, arguments);<br>        &#125;<br>    &#125;;<br>&#125;<br></code></pre></td></tr></table></figure>


<p>Invoker -&gt; Exporter</p>
<p>ProtocolFilterWrapper.export()方法中</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> &lt;T&gt; Exporter&lt;T&gt; <span class="hljs-title function_">export</span><span class="hljs-params">(Invoker&lt;T&gt; invoker)</span> <span class="hljs-keyword">throws</span> RpcException &#123;<br>    <span class="hljs-comment">// 注册中心</span><br>    <span class="hljs-keyword">if</span> (Constants.REGISTRY_PROTOCOL.equals(invoker.getUrl().getProtocol())) &#123;<br>        <span class="hljs-keyword">return</span> protocol.export(invoker); <span class="hljs-comment">// 向注册中心发布服务的时候并不会组装 filter 调用链</span><br>    &#125;<br>    <span class="hljs-comment">// 建立带有 Filter 过滤链的 Invoker,再暴露服务</span><br>    <span class="hljs-keyword">return</span> protocol.export(buildInvokerChain(invoker, Constants.SERVICE_FILTER_KEY, Constants.PROVIDER));<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 创建带 Filter 链的 Invoker 对象</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> invoker Invoker 对象</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> key 获取 URL 参数名</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> group 分组</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> &lt;T&gt; 泛型</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@return</span> Invoker 对象</span><br><span class="hljs-comment">    */</span><br><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> &lt;T&gt; Invoker&lt;T&gt; <span class="hljs-title function_">buildInvokerChain</span><span class="hljs-params">(<span class="hljs-keyword">final</span> Invoker&lt;T&gt; invoker, String key, String group)</span> &#123;<br>    Invoker&lt;T&gt; last = invoker;<br>    <span class="hljs-comment">// 获得匹配激活的过滤器数组</span><br>    List&lt;Filter&gt; filters = ExtensionLoader.getExtensionLoader(Filter.class).getActivateExtension(invoker.getUrl(), key, group);<br>    <span class="hljs-comment">// 倒序循环 Filter,创建带 Filter 链的 Invoker 对象</span><br>    <span class="hljs-keyword">if</span> (!filters.isEmpty()) &#123;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> filters.size() - <span class="hljs-number">1</span>; i &gt;= <span class="hljs-number">0</span>; i--) &#123;<br>            <span class="hljs-keyword">final</span> <span class="hljs-type">Filter</span> <span class="hljs-variable">filter</span> <span class="hljs-operator">=</span> filters.get(i);<br>            <span class="hljs-keyword">final</span> Invoker&lt;T&gt; next = last;<br>            last = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Invoker</span>&lt;T&gt;() &#123;<br><br>                <span class="hljs-meta">@Override</span><br>                <span class="hljs-keyword">public</span> Class&lt;T&gt; <span class="hljs-title function_">getInterface</span><span class="hljs-params">()</span> &#123;<br>                    <span class="hljs-keyword">return</span> invoker.getInterface();<br>                &#125;<br><br>                <span class="hljs-meta">@Override</span><br>                <span class="hljs-keyword">public</span> URL <span class="hljs-title function_">getUrl</span><span class="hljs-params">()</span> &#123;<br>                    <span class="hljs-keyword">return</span> invoker.getUrl();<br>                &#125;<br><br>                <span class="hljs-meta">@Override</span><br>                <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">isAvailable</span><span class="hljs-params">()</span> &#123;<br>                    <span class="hljs-keyword">return</span> invoker.isAvailable();<br>                &#125;<br><br>                <span class="hljs-meta">@Override</span><br>                <span class="hljs-keyword">public</span> Result <span class="hljs-title function_">invoke</span><span class="hljs-params">(Invocation invocation)</span> <span class="hljs-keyword">throws</span> RpcException &#123;<br>                    <span class="hljs-keyword">return</span> filter.invoke(next, invocation);<br>                &#125;<br><br>                <span class="hljs-meta">@Override</span><br>                <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">destroy</span><span class="hljs-params">()</span> &#123;<br>                    invoker.destroy();<br>                &#125;<br><br>                <span class="hljs-meta">@Override</span><br>                <span class="hljs-keyword">public</span> String <span class="hljs-title function_">toString</span><span class="hljs-params">()</span> &#123;<br>                    <span class="hljs-keyword">return</span> invoker.toString();<br>                &#125;<br>            &#125;;<br>        &#125;<br>    &#125;<br>    <span class="hljs-keyword">return</span> last;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>ProtocolListenerWrapper.export()</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> &lt;T&gt; Exporter&lt;T&gt; <span class="hljs-title function_">export</span><span class="hljs-params">(Invoker&lt;T&gt; invoker)</span> <span class="hljs-keyword">throws</span> RpcException &#123;<br>    <span class="hljs-comment">// 注册中心协议</span><br>    <span class="hljs-keyword">if</span> (Constants.REGISTRY_PROTOCOL.equals(invoker.getUrl().getProtocol())) &#123;<br>        <span class="hljs-keyword">return</span> protocol.export(invoker);<br>    &#125;<br>    <span class="hljs-comment">// 暴露服务,创建 Exporter 对象</span><br>    Exporter&lt;T&gt; exporter = protocol.export(invoker);<br>    <span class="hljs-comment">// 获得 ExporterListener 数组</span><br>    List&lt;ExporterListener&gt; listeners = Collections.unmodifiableList(ExtensionLoader.getExtensionLoader(ExporterListener.class).getActivateExtension(invoker.getUrl(), Constants.EXPORTER_LISTENER_KEY));<br>    <span class="hljs-comment">// 创建带 ExporterListener 的 Exporter 对象</span><br>    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListenerExporterWrapper</span>&lt;T&gt;(exporter, listeners);<br>&#125;<br></code></pre></td></tr></table></figure>

<p>InjvmProtocol.export()</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> &lt;T&gt; Exporter&lt;T&gt; <span class="hljs-title function_">export</span><span class="hljs-params">(Invoker&lt;T&gt; invoker)</span> <span class="hljs-keyword">throws</span> RpcException &#123;<br>    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">InjvmExporter</span>&lt;T&gt;(invoker, invoker.getUrl().getServiceKey(), exporterMap);<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="使用本地暴露的原因"><a href="#使用本地暴露的原因" class="headerlink" title="使用本地暴露的原因"></a>使用本地暴露的原因</h3><p>本地调用使用了 injvm 协议,是一个伪协议,它不开启端口,不发起远程调用,只在 JVM 内直接关联,但执行 Dubbo 的 Filter 链.</p>
<h2 id="服务远程暴露"><a href="#服务远程暴露" class="headerlink" title="服务远程暴露"></a>服务远程暴露</h2><p>服务远程暴露的总体流程:</p>
<p>1.将 ref 封装为 Invoker</p>
<p>2.将 Invoker 转换为 Exporter</p>
<p>3.启动 Netty 服务端</p>
<p>4.注册 provider 到 zk</p>
<p>5.zk 订阅与通</p>
<p>6.返回 Exporter 实例</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><code class="hljs plaintext">- 服务远程暴露<br>- 使用 ProxyFactory 将 ref 创建成 Invoker 对象(这部分逻辑和本地暴露类似)<br>    - ProxyFactory$Adaptive.getInvoker()中<br>        - extension.getInvoker(arg0, arg1, arg2);<br>            - StubProxyFactoryWrapper.getInvoker()直接调用内部的JavassistProxyFactory.getInvoker()<br>                - JavassistProxyFactory.getInvoker()方法中<br>                - 1.根据传入的 proxy对象(如DemoServiceImpl)的类信息创建对它的包装对象Wrapper<br>                - 2.根据proxy, type, url创建AbstractProxyInvoker对象,它的doInvoke()方法会调用wrapper类的invokeMethod()方法<br>- 首先将AbstractProxyInvoker实例包装成DelegateProviderMetaDataInvoker实例<br>- 使用 Protocol 暴露 Invoker 对象为 Exporter<br>    - Protocol$Adaptive.export()中<br>    - 1.此时从传入的invoker.url.protocol获取到的协议为&quot;registry&quot;,获取到的Protocol拓展点实现类为包裹了DubboProtocol 对象的 ProtocolFilterWrapper<br>    - 2.extension.export(invoker)<br>        - ProtocolFilterWrapper.export()这个类在Invoker基础上添加filter链<br>            - 1.因为传入的协议时&quot;registry&quot;,向注册中心暴露服务<br>                - ProtocolListenerWrapper.export()中<br>                - 1.调用RegistryProtocol.export(),向注册中心注册<br>                    - RegistryProtocol.export()中<br>                        - 1.doLocalExport()本地启动服务<br>                            - RegistryProtocol.doLocalExport()中<br>                            - 1.尝试从bounds缓存中获取exporter<br>                            - 2.若1中获取不到则创建exporter并缓存<br>                            - 3.根据invoker创建InvokerDelegete委托类,提供 获取 invoker 的方法<br>                            - 4.调用Protocol$Adaptive.export()方法<br>                                - Protocol$Adaptive.export()这次传入的协议是&quot;dubbo&quot;<br>                                    - ProtocolFilterWrapper.export()中<br>                                    - 1.buildInvokerChain()构建filter链<br>                                    - 2.protocol.export()调用DubboProtocol.export()<br>                                        - DubboProtocol.export()中<br>                                        - 1.通过invoker创建DubboExporter<br>                                        - // 2.参见netty启动流程<br>                            - 5.将4中返回的exporter包装成ExporterChangeableWrapper<br>                        - 获取注册中心,如zk<br>                            - 向注册中心注册自己<br>                                - FailbackRegistry.register(url)<br></code></pre></td></tr></table></figure>

<p>ProtocolFilterWrapper.export()中</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> &lt;T&gt; Exporter&lt;T&gt; <span class="hljs-title function_">export</span><span class="hljs-params">(Invoker&lt;T&gt; invoker)</span> <span class="hljs-keyword">throws</span> RpcException &#123;<br>    <span class="hljs-comment">// 注册中心</span><br>    <span class="hljs-keyword">if</span> (Constants.REGISTRY_PROTOCOL.equals(invoker.getUrl().getProtocol())) &#123;<br>        <span class="hljs-keyword">return</span> protocol.export(invoker);<span class="hljs-comment">// 向注册中心发布服务的时候并不会组装 filter调用链</span><br>    &#125;<br>    <span class="hljs-comment">// 建立带有 Filter 过滤链的 Invoker,再暴露服务</span><br>    <span class="hljs-keyword">return</span> protocol.export(buildInvokerChain(invoker, Constants.SERVICE_FILTER_KEY, Constants.PROVIDER));<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> &lt;T&gt; Exporter&lt;T&gt; <span class="hljs-title function_">export</span><span class="hljs-params">(<span class="hljs-keyword">final</span> Invoker&lt;T&gt; originInvoker)</span> <span class="hljs-keyword">throws</span> RpcException &#123;<br>    <span class="hljs-comment">// 暴露服务</span><br>    <span class="hljs-comment">// export invoker</span><br>    <span class="hljs-keyword">final</span> ExporterChangeableWrapper&lt;T&gt; exporter = doLocalExport(originInvoker);<br><br>    <span class="hljs-comment">// 获得注册中心 URL</span><br>    <span class="hljs-type">URL</span> <span class="hljs-variable">registryUrl</span> <span class="hljs-operator">=</span> getRegistryUrl(originInvoker);<br><br>    <span class="hljs-comment">// 获得注册中心对象(启动zookeeper)</span><br>    <span class="hljs-comment">// registry provider</span><br>    <span class="hljs-keyword">final</span> <span class="hljs-type">Registry</span> <span class="hljs-variable">registry</span> <span class="hljs-operator">=</span> getRegistry(originInvoker);<br><br>    <span class="hljs-comment">// 获得服务提供者 URL</span><br>    <span class="hljs-keyword">final</span> <span class="hljs-type">URL</span> <span class="hljs-variable">registedProviderUrl</span> <span class="hljs-operator">=</span> getRegistedProviderUrl(originInvoker);<br><br>    <span class="hljs-comment">//to judge to delay publish whether or not.判断是否延迟发布</span><br>    <span class="hljs-type">boolean</span> <span class="hljs-variable">register</span> <span class="hljs-operator">=</span> registedProviderUrl.getParameter(<span class="hljs-string">&quot;register&quot;</span>, <span class="hljs-literal">true</span>);<br><br>    <span class="hljs-comment">// 向注册中心订阅服务消费者</span><br>    ProviderConsumerRegTable.registerProvider(originInvoker, registryUrl, registedProviderUrl);<br><br>    <span class="hljs-comment">// 向注册中心注册服务提供者(自己)</span><br>    <span class="hljs-keyword">if</span> (register) &#123;<br>        register(registryUrl, registedProviderUrl);<br>        ProviderConsumerRegTable.getProviderWrapper(originInvoker).setReg(<span class="hljs-literal">true</span>); <span class="hljs-comment">// 标记向本地注册表的注册服务提供者,已经注册</span><br>    &#125;<br><br>    <span class="hljs-comment">// 使用 OverrideListener 对象,订阅配置规则</span><br>    <span class="hljs-comment">// Subscribe the override data</span><br>    <span class="hljs-comment">// FIXME When the provider subscribes, it will affect the scene : a certain JVM exposes the service and call the same service. Because the subscribed is cached key with the name of the service, it causes the subscription information to cover.</span><br>    <span class="hljs-comment">// 创建订阅配置规则的 URL</span><br>    <span class="hljs-keyword">final</span> <span class="hljs-type">URL</span> <span class="hljs-variable">overrideSubscribeUrl</span> <span class="hljs-operator">=</span> getSubscribedOverrideUrl(registedProviderUrl);<br>    <span class="hljs-comment">// 创建 OverrideListener 对象,并添加到 `overrideListeners` 中</span><br>    <span class="hljs-keyword">final</span> <span class="hljs-type">OverrideListener</span> <span class="hljs-variable">overrideSubscribeListener</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">OverrideListener</span>(overrideSubscribeUrl, originInvoker);<br>    overrideListeners.put(overrideSubscribeUrl, overrideSubscribeListener);<br>    <span class="hljs-comment">// 向注册中心,发起订阅</span><br>    registry.subscribe(overrideSubscribeUrl, overrideSubscribeListener);<br>    <span class="hljs-comment">//Ensure that a new exporter instance is returned every time export</span><br>    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">DestroyableExporter</span>&lt;T&gt;(exporter, originInvoker, overrideSubscribeUrl, registedProviderUrl);<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="Netty"><a href="#Netty" class="headerlink" title="Netty"></a>Netty</h2><p>ExchangeHandlerAdapter 定义了与客户端连接成功／断开连接／接受到客户端消息／响应消息,以及创造Invocation的方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> ExchangeServer <span class="hljs-title function_">bind</span><span class="hljs-params">(URL url, ExchangeHandler handler)</span> <span class="hljs-keyword">throws</span> RemotingException &#123;<br>    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">HeaderExchangeServer</span>(Transporters.bind(url, <span class="hljs-keyword">new</span> <span class="hljs-title class_">DecodeHandler</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">HeaderExchangeHandler</span>(handler))));<br>&#125;<br></code></pre></td></tr></table></figure>
<p>HeaderExchangeHandler: 基于消息头(Header)的信息交换服务器实现类</p>
<p>DecodeHandler: 解码处理器,处理接收到的消息</p>
<h3 id="Netty-启动流程"><a href="#Netty-启动流程" class="headerlink" title="Netty 启动流程"></a>Netty 启动流程</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs plaintext">- netty启动流程<br>- openServer(url)启动通信服务器(如Netty)<br>    - DubboProtocol.openServer()中<br>        - DubboProtocol.createServer()中<br>        - 1.创建ExchangeHandlerAdapter实例,作为请求处理器<br>        - 2.Exchangers.bind(url,requestHandler)<br>            - Exchangers.bind()中<br>            - 1.根据url获取Exchanger,返回的是HeaderExchanger对象<br>                - HeaderExchanger.bind()<br>                    - HeaderExchanger.bind()方法中<br>                    - 1. return new HeaderExchangeServer(Transporters.bind(url, new DecodeHandler(new HeaderExchangeHandler(handler))))会对传入的ExchangeHandlerAdapter包装两层,然后使用Transporters.bind()创建NettyServer对象.<br>                        - Transporters.bind()中<br>                        - 1.返回Transporter的适配对象<br>                        - 2.调用Transporter$Adaptive.bind()<br>                            - netty4/NettyTransporter.bind()通过new NettyServer(url, listener)创建nettyServer对象<br>                                - NettyServer(url,handler)构造器中会通过ChannelHandlers会传入的DecodeHandler进行包装<br>                                    - ChannelHandler.wrapInternal()方法中<br>                                    - 1.获取Dispatcher的适配对象<br>                                    - 2.调用Dispatcher.dispatch(handler, url)<br>                                        - AllDispatcher.dispatch()中new AllChannelHandler(handler, url)<br>                                            - AllChannelHandler调用父类构造方法WrappedChannelHandler<br>                                                - WrappedChannelHandler()构造方法中<br>                                                - 1.创建线程池<br>                                    - 3.把2中的返回结果用HeartbeatHandler和MultiMessageHandler再包装<br>                                - NettyServer.doOpen()<br>                - 1.创建<br>            - 2.exchanger.bind()<br>    - ExchangeServer.createServer() 创建ExchangeServer<br></code></pre></td></tr></table></figure>

<h2 id="Zookeeper"><a href="#Zookeeper" class="headerlink" title="Zookeeper"></a>Zookeeper</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-title function_">ZookeeperRegistry</span><span class="hljs-params">(URL url, ZookeeperTransporter zookeeperTransporter)</span> &#123;<br>    <span class="hljs-built_in">super</span>(url);<br>    <span class="hljs-keyword">if</span> (url.isAnyHost()) &#123;<br>        <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">IllegalStateException</span>(<span class="hljs-string">&quot;registry address == null&quot;</span>);<br>    &#125;<br>    <span class="hljs-comment">// 获得 Zookeeper 根节点</span><br>    <span class="hljs-type">String</span> <span class="hljs-variable">group</span> <span class="hljs-operator">=</span> url.getParameter(Constants.GROUP_KEY, DEFAULT_ROOT); <span class="hljs-comment">// `url.parameters.group` 参数值</span><br>    <span class="hljs-keyword">if</span> (!group.startsWith(Constants.PATH_SEPARATOR)) &#123;<br>        group = Constants.PATH_SEPARATOR + group;<br>    &#125;<br>    <span class="hljs-built_in">this</span>.root = group;<br>    <span class="hljs-comment">// 创建 Zookeeper Client</span><br>    zkClient = zookeeperTransporter.connect(url);<br>    <span class="hljs-comment">// 添加 StateListener 对象.该监听器,在重连时,调用恢复方法.</span><br>    zkClient.addStateListener(<span class="hljs-keyword">new</span> <span class="hljs-title class_">StateListener</span>() &#123;<br>        <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">stateChanged</span><span class="hljs-params">(<span class="hljs-type">int</span> state)</span> &#123;<br>            <span class="hljs-keyword">if</span> (state == RECONNECTED) &#123;<br>                <span class="hljs-keyword">try</span> &#123;<br>                    recover();<br>                &#125; <span class="hljs-keyword">catch</span> (Exception e) &#123;<br>                    logger.error(e.getMessage(), e);<br>                &#125;<br>            &#125;<br>        &#125;<br>    &#125;);<br>&#125;<br></code></pre></td></tr></table></figure>

<p>AbstractRegistry: </p>
<ul>
<li>1.通用的注册,订阅,查询,通知等方法</li>
<li>2.读取和持久化注册数据到文件,以 properties 格式存储</li>
</ul>
<p>FailbackRegistry:<br>主要用来做失败重试操作(包括: 注册失败,反注册失败,订阅失败,反订阅失败,通知失败的重试);也提供了供 ZookeeperRegistry 使用的 zk 重连后的恢复工作的方法.</p>
<p>ZookeeperRegistry:<br>创建 zk 客户端,启动会话;并且调用 FailbackRegistry 实现 zk 重连后的恢复工作.</p>
<h3 id="注册到-zk-流程"><a href="#注册到-zk-流程" class="headerlink" title="注册到 zk 流程"></a>注册到 zk 流程</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs plaintext">- 注册到zk流程<br>- RegistryProtocol.export()<br>- 1.获得注册中心 URL<br>    - 就是把url改为zookeeper://协议开头<br>- 2.获得注册中心对象(启动zookeeper),通过ZookeeperRegistryFactory实例创建并返回ZookeeperRegistry对象<br>    - AbstractRegistryFactory.getRegistry(url)中<br>    - 1.加锁 从缓存中获得 Registry 对象<br>    - 2.若获取不到,则createRegistry(url)创建 Registry 对象并缓存<br>        - ZookeeperRegistryFactory.createRegistry()中会new ZookeeperRegistry(url, zookeeperTransporter)创建ZookeeperRegistry实例<br>- 3.获得服务提供者 URL<br>- 4.向注册中心注册服务提供者<br>    - RegistryProtocol.register(registedProviderUrl)<br>    - 1.从registryFactory中获取registry<br>    - 2.registry.register()<br>        - ZookeeperRegistry.doRegister()<br>        - 1.通过url得到zk路径,创建临时节点<br>- 5.向注册中心发起订阅<br>    当前的provider订阅了/dubbo/com.alibaba.dubbo.demo.DemoService/configurators,当其下的子节点发生变化(url)时,通知到provider,使得providerUrl发生变化,则提供者需要重新暴露<br>    - RegistryProtocol.export()<br>        - FailbackRegistry.subscribe()<br>            - ZookeeperRegistry.doSubscribe()<br>            - 创建持久节点/dubbo/com.alibaba.dubbo.demo.DemoService/configurators<br>- 6.返回Exporter<br></code></pre></td></tr></table></figure>
<link rel="stylesheet" href="https://fastly.jsdelivr.net/npm/markmap-toolbar@0.18.10/dist/style.css"><script src="https://fastly.jsdelivr.net/npm/d3@7"></script><script src="https://fastly.jsdelivr.net/npm/markmap-view@0.18.10"></script><script src="https://fastly.jsdelivr.net/npm/markmap-toolbar@0.18.10"></script>
<link rel="stylesheet" href="/css/markmap.css">

<script src="/js/markmap.js"></script>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://flyingzc.github.io">flyingzc</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://flyingzc.github.io/articles/dubbo/dubbo03-export.html">https://flyingzc.github.io/articles/dubbo/dubbo03-export.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来源 <a href="https://flyingzc.github.io" target="_blank">flyingzc's blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Dubbo/">Dubbo</a></div><div class="post-share"><div class="social-share" data-image="/images/zc-icon.jpeg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><a class="pagination-related" href="/articles/dubbo/dubbo01-overview.html" title="Dubbo 整理"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2019-11-26</div><div class="info-item-2">Dubbo 整理</div></div><div class="info-2"><div class="info-item-1">整理自官网, 对整体理解 Dubbo 很有作用. 作用Apache Dubbo 是一款高性能, 轻量级的 Java RPC 框架. 三大核心能力面向接口的远程方法调用 智能容错和负载均衡 以及服务自动注册和发现 健壮性监控中心宕掉不影响使用, 只是丢失部分采样数据. 数据库宕掉后, 注册中心仍能通过缓存提供服务列表查询, 但不能注册新服务. 注册中心对等集群, 任意一台宕掉后, 将自动切换到另一台. 注册中心全部宕掉后, 服务提供者 和 服务消费者 仍能通过本地缓存通讯. 服务提供者无状态, 任意一台宕掉后, 不影响使用. 服务提供者全部宕掉后, 服务消费者应用将无法使用, 并 无限次重连等待服务提供者恢复. 伸缩性注册中心为对等集群, 可动态增加机器部署实例, 所有客户端将自动发现新的注册中心. 服务提供者无状态, 可动态增加机器部署实例, 注册中心将推送新的服务提供者信息给消费者. 协议 Dubbo 协议采用 NIO 复用单一长连接, 并使用线程池并发处理请求, 减少握手和加大并发效率, 性能较好(推荐使用)在大文件传输时, 单一连接会成为瓶颈可用于生产环境  Rmi...</div></div></div></a><a class="pagination-related" href="/articles/dubbo/dubbo02-spi.html" title="Dubbo SPI 机制"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2019-11-27</div><div class="info-item-2">Dubbo SPI 机制</div></div><div class="info-2"><div class="info-item-1">SPI 机制SPI 作用SPI 全称为 Service Provider Interface, 是一种服务发现机制. SPI 的本质是将接口实现类的全限定名配置在文件中, 并由服务加载器读取配置文件, 加载实现类. 这样可以在运行时, 动态为接口替换实现类. 正因此特性, 我们可以很容易的通过 SPI 机制为我们的程序提供拓展功能. Dubbo 里面有很多个组件, 每个组件在框架中都是以接口的形式抽象出来. 具体的实现又分很多种, 在程序执行时根据用户的配置来按需取接口的实现类. Dubbo 实现的 SPI 与 JDK 自带的 SPI 的区别摘自官网  JDK 标准的 SPI 会一次性实例化扩展点所有实现, 如果有扩展实现初始化很耗时, 但如果没用上也加载, 会很浪费资源.  如果扩展点加载失败, 连扩展点的名称都拿不到了.比如: JDK 标准的 ScriptEngine, 通过 getName() 获取脚本类型的名称, 但如果 RubyScriptEngine 因为所依赖的 jruby.jar 不存在, 导致 RubyScriptEngine 类加载失败,...</div></div></div></a><a class="pagination-related" href="/articles/dubbo/dubbo04-refer.html" title="Dubbo 服务引用"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2019-11-29</div><div class="info-item-2">Dubbo 服务引用</div></div><div class="info-2"><div class="info-item-1">官网 客户端服务引用客户端服务引用 demo12DemoService demoService = (DemoService) context.getBean(&quot;demoService&quot;); // get remote service proxydemoService.sayHello(&quot;world&quot;); dubbo 在客户端动态的生成了一个该 Interface 类型的代理类.在这个代理类中封装了远程服务调用的组件. DemoService 创建12&lt;dubbo:reference id=&quot;demoService&quot; group=&quot;g1&quot; check=&quot;true&quot; interface=&quot;com.alibaba.dubbo.demo.DemoService&quot;                     client=&quot;netty4&quot; timeout=&quot;10000&quot; callbacks=&quot;1000&quot;...</div></div></div></a><a class="pagination-related" href="/articles/dubbo/dubbo05-cluster.html" title="Dubbo Cluster"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2019-12-04</div><div class="info-item-2">Dubbo Cluster</div></div><div class="info-2"><div class="info-item-1">clusterCluster 将 Directory 中的多个 Invoker 伪装成一个 Invoker,对上层透明,伪装过程包含了容错逻辑,调用失败后,重试另一个. 集群模块处于服务提供者和消费者之间,对于服务消费者来说,集群可向其屏蔽服务提供者集群的情况,使其能够专心进行远程调用. cluster 继承结构 12345678910Cluster(com.alibaba.dubbo.rpc.cluster)    MergeableCluster(com.alibaba.dubbo.rpc.cluster.support)    FailfastCluster(com.alibaba.dubbo.rpc.cluster.support)    MockClusterWrapper(com.alibaba.dubbo.rpc.cluster.support.wrapper)    FailsafeCluster(com.alibaba.dubbo.rpc.cluster.support)   ...</div></div></div></a><a class="pagination-related" href="/articles/dubbo/dubbo06-provider.html" title="Dubbo Provider处理请求"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2019-12-05</div><div class="info-item-2">Dubbo Provider处理请求</div></div><div class="info-2"><div class="info-item-1">服务端接收并响应请求流程provider处理请求流程  123456789101112- NettyServerWorker线程接收请求    - ByteToMessageDecoder.channelRead()        - ByteToMessageDecoder.callDecode()            - NettyCodecAdapter.InternalDecoder.decode()                - DubboCountCodec.decode()解码        - fireChannelRead(ctx, out, size)            - NettyServerHandler.channelRead()                - AbstractPeer.received()                    - MultiMessageHandler.received()                        - HeartbeatHandler.received()            ...</div></div></div></a><a class="pagination-related" href="/articles/dubbo/dubbo07-heartbeat.html" title="Dubbo心跳机制"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2019-12-15</div><div class="info-item-2">Dubbo心跳机制</div></div><div class="info-2"><div class="info-item-1">Dubbo心跳机制Dubbo 默认客户端和服务端都会发送心跳报文,用来保持 TCP 长连接状态. 在客户端和服务端,Dubbo 都会开启一个线程 循环扫描 并 检测连接是否超时. 在服务端发现超时则会主动关闭客户端连接. 在客户端发现超时则会主动重新创建连接. 默认心跳检测时间是 60 秒. 原理provider 心跳默认是在 heartbeat(一分钟)内如果没有读消息或写消息,就会发送心跳请求消息,如果连着3次(180s)没有读消息,provider会关闭channel. consumer 端的心跳默认是在 一分钟 内如果没有读消息或写消息,就会发送心跳请求消息,如果连着3次(三分钟)没有读消息,consumer会进行重连. 开启定时任务provider 在启动 netty时,在 HeaderExchangeServer 的构造方法中,会通过 startHeatbeatTimer() 启动心跳定时任务.consumer 在 HeaderExchangeClient 的构造方法里,同样通过 startHeatbeatTimer()...</div></div></div></a></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info text-center"><div class="avatar-img"><img src="/images/zc-icon.jpeg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info-name">flyingzc</div><div class="author-info-description">不积跬步无以至千里<br/>不积小流无以成江海</div><div class="site-data"><a href="/archives/"><div class="headline">文章</div><div class="length-num">84</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">36</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">29</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/FlyingZC"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons"><a class="social-icon" href="https://github.com/FlyingZC" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">FlyingZC's Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Dubbo-%E4%B8%8E-Spring-%E7%9A%84%E7%BB%93%E5%90%88"><span class="toc-number">1.</span> <span class="toc-text">Dubbo 与 Spring 的结合</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#META-INF-dubbo-xsd"><span class="toc-number">1.1.</span> <span class="toc-text">META-INF&#x2F;dubbo.xsd</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#META-INF-spring-schemas"><span class="toc-number">1.2.</span> <span class="toc-text">META-INF&#x2F;spring.schemas</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#META-INF-spring-handlers"><span class="toc-number">1.3.</span> <span class="toc-text">META-INF&#x2F;spring.handlers</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#DubboNamespaceHandler-init"><span class="toc-number">1.4.</span> <span class="toc-text">DubboNamespaceHandler.init()</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#DubboBeanDefinitionParser"><span class="toc-number">1.5.</span> <span class="toc-text">DubboBeanDefinitionParser</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#ServiceBean"><span class="toc-number">2.</span> <span class="toc-text">ServiceBean</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9C%8D%E5%8A%A1%E6%9C%AC%E5%9C%B0%E6%9A%B4%E9%9C%B2"><span class="toc-number">2.1.</span> <span class="toc-text">服务本地暴露</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E6%9C%AC%E5%9C%B0%E6%9A%B4%E9%9C%B2%E7%9A%84%E5%8E%9F%E5%9B%A0"><span class="toc-number">2.1.1.</span> <span class="toc-text">使用本地暴露的原因</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9C%8D%E5%8A%A1%E8%BF%9C%E7%A8%8B%E6%9A%B4%E9%9C%B2"><span class="toc-number">2.2.</span> <span class="toc-text">服务远程暴露</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Netty"><span class="toc-number">2.3.</span> <span class="toc-text">Netty</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Netty-%E5%90%AF%E5%8A%A8%E6%B5%81%E7%A8%8B"><span class="toc-number">2.3.1.</span> <span class="toc-text">Netty 启动流程</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Zookeeper"><span class="toc-number">2.4.</span> <span class="toc-text">Zookeeper</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B3%A8%E5%86%8C%E5%88%B0-zk-%E6%B5%81%E7%A8%8B"><span class="toc-number">2.4.1.</span> <span class="toc-text">注册到 zk 流程</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/ignite/ignite03-transaction-begin.html" title="Apache Ignite 开启事务执行流程">Apache Ignite 开启事务执行流程</a><time datetime="2025-10-10T16:00:00.000Z" title="发表于 2025-10-11 00:00:00">2025-10-11</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/ignite/ignite02-transaction.html" title="Apache Ignite 分布式事务原理">Apache Ignite 分布式事务原理</a><time datetime="2025-09-17T16:00:00.000Z" title="发表于 2025-09-18 00:00:00">2025-09-18</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/ignite/ignite01-build.html" title="Apache Ignite IDEA 环境搭建">Apache Ignite IDEA 环境搭建</a><time datetime="2025-09-10T16:00:00.000Z" title="发表于 2025-09-11 00:00:00">2025-09-11</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/druid/druid01-resultset-leak.html" title="ShardingSphere JDBC 使用 Druid 连接池 ResultSet 内存泄露分析">ShardingSphere JDBC 使用 Druid 连接池 ResultSet 内存泄露分析</a><time datetime="2025-06-30T16:00:00.000Z" title="发表于 2025-07-01 00:00:00">2025-07-01</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/jmeter/jmeter01-shardingsphere.html" title="Jmeter 压测 ShardingSphere">Jmeter 压测 ShardingSphere</a><time datetime="2025-06-30T16:00:00.000Z" title="发表于 2025-07-01 00:00:00">2025-07-01</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2025 By flyingzc</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="日间和夜间模式切换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>